/**
  * This file is part of Medusa.
  *
  * Authors: Uriel Fanelli <uriel.fanelli@gmail.com>
  *          Perthect <perthect@gmail.com>
  *          Marco Bizzarri <Marco.Bizzarri@gmail.com>
  *
  *
  * Medusa is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.

  * Medusa is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.

  * You should have received a copy of the GNU General Public License
  * along with Medusa; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */


        
#include "tabwidget.h"
#include "singletab.h"
#include "tabfactory.h"
#include "gnetworkmanager.h"

TabWidget::TabWidget(GProgressBar *finestra,QTAstbar *x)
{
    progressBar=finestra;
    qsb=x;

    setMovable(true);
    setTabsClosable(true);
    connect(this,SIGNAL(tabCloseRequested(int)),this,SLOT(chiuditab(int)));
    this->manager = new GNetworkAccessManager;
}

TabWidget::~TabWidget()
{
    for (int i = 0; i < count(); i++) {
        SingleTab *st = (SingleTab*) widget(i);
        removeTab(i);
        delete st;
    }
}

void TabWidget::createTabForURL(QUrl url, QIcon icon, QString label)
{
    stab = TabFactory::create(url, this, this->manager, progressBar);
    addTab(stab, icon, label);
    setTabToolTip(count() - 1, url.toString());
    setCurrentIndex(count() - 1);
    connect(stab, SIGNAL(urlChanged(QUrl)), this, SLOT(aggiornatabtip(QUrl)));
    qsb->setViewName(url);
    connect(stab, SIGNAL(loadStarted()),      qsb, SLOT(pageLoadStarted()));
    connect(stab, SIGNAL(loadProgress(int)),  qsb, SLOT(pageLoadInProgress(int)));
    connect(stab, SIGNAL(loadFinished(bool)), qsb, SLOT(pageLoadFinished(bool)));
}


int TabWidget::findMatchingTab(QUrl url)
{
    for (int i = 0; i < count(); ++i) {
        QWidget *widget = this->widget(i);
        if (SingleTab *tab = qobject_cast<SingleTab*>(widget)){
            if(tab->cometichiami() == url)
                return i;
         }
    }
    return count();
}

// aggiungitab ONLY IF NOT ON ALREADY
void TabWidget::aggiungitab(QUrl url, QIcon icon, QString label)
{
    int tabIndex = findMatchingTab(url);
    if (tabIndex == count()) {
        createTabForURL(url, icon, label);
    } else {
        setCurrentIndex(tabIndex);
    }
}

void TabWidget::aggiornatab()
{
    QWidget *widget = this->widget(currentIndex());
    if (SingleTab *tab = qobject_cast<SingleTab*>(widget)){
        tab->reload();
        qsb->setViewName(tab->url());
    }
}

void TabWidget::pageback()
{
    QWidget *widget = this->widget(currentIndex());
    if (SingleTab *tab = qobject_cast<SingleTab*>(widget)){
        tab->back();
        qsb->setViewName(tab->url());
    }
}

void TabWidget::pageforward()
{
    QWidget *widget = this->widget(currentIndex());
    if (SingleTab *tab = qobject_cast<SingleTab*>(widget)){
        tab->forward();
        qsb->setViewName(tab->url());
    }
}

void TabWidget::stopload()
{
    QWidget *widget = this->widget(currentIndex());
    if (SingleTab *tab = qobject_cast<SingleTab*>(widget)){
        tab->stop();
        qsb->setViewName(tab->url());
    }
}

void TabWidget::aggiornatabtip(QUrl url)
{
    setTabToolTip(currentIndex(), url.toString());           // TAB TIP UPDATE
    QWidget *widget = this->widget(currentIndex());
    if (SingleTab *tab = qobject_cast<SingleTab*>(widget))
        qsb->setViewName(tab->url());          // STATUS BAR UPDATE
}

void TabWidget::chiuditab(int index)
{
    QWidget *widget = this->widget(index);
    SingleTab *tab = qobject_cast<SingleTab*>(widget);
    removeTab(index);
    if (tab != 0)
        delete tab;
}
